/*
 * Filename:    ResultToldDocument.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-12-8
 */
package com.paic.mhis.hcpms.warning.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.paic.mhis.hcpms.itextpdf.web.pdfmodel.DocumentModel;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsFontUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsParagraphUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfCellUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfTableUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsRectangleUtil;

public class ResultToldDocument implements DocumentModel {
	public static final String WARN_NO = "预警通知单号:";
	public static final String UNIT_NAME = "单位名称";
	public static final String UNIT_PHONE = "联系电话";
	public static final String UNIT_ADDRESS = "单位地址";
	public static final String UNIT_PROBLEM = "存在问题";
	public static final String UNIT_DICIDE = "处理决定";
	public static final String UNIT_AMOUNT = "不合理费用:";
	public static final String WARN_PRIOD = "预警时段";
	public static final String WARN_RESULT = "预警结果";
	public static final String WARN_FROM_UNIT = "社保经办科室(盖章):";
	public static final String WARN_PARENT_DATE = "  年       月       日";
	public static final String WARN_TO_UNIT = "定点单位(盖章):";
	public static final String AUDIT_UNIT = "经办科室电话:";
	private static final String TITLE_NAME = "定点机构预警结果告知书";

	@Override
	public void createDocument(Document document) {
		document.setPageSize(HcpmsRectangleUtil.createNoticeRect());

	}

	@Override
	public void createContent(Document document, Map<String, Object> map) {
		Font font = HcpmsFontUtil.createTitleFont(12, Font.BOLD);
		Font fontText = HcpmsFontUtil.createTextFont(10, Font.NORMAL);
		try {
			float[] widths = { 0.1f, 0.1f, 0.3f, 0.1f, 0.4f };
			PdfPTable table = HcpmsPdfTableUtil.createTable(widths, 25, 95, Element.ALIGN_CENTER);
			table.addCell(HcpmsPdfCellUtil.createCellCol(HcpmsParagraphUtil.createParagraphText(font, TITLE_NAME),
					new int[] { 4, 4, 4, 4 }, Element.ALIGN_CENTER, 5, Rectangle.NO_BORDER, 30f));

			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_NO + getValue(map, "warnNo")), new int[] { 1,
							1, 1, 1 }, Element.ALIGN_LEFT, 5, Rectangle.NO_BORDER, 10f));

			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil.createParagraphText(fontText, UNIT_NAME),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_RIGHT));
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "name")), new int[] { 4, 4, 4, 2 },
					Element.ALIGN_LEFT, 2));
			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil.createParagraphText(fontText, UNIT_PHONE),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_RIGHT));
			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "phone")), new int[] { 4, 4, 4, 2 },
					Element.ALIGN_LEFT));

			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil.createParagraphText(fontText, UNIT_ADDRESS),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_RIGHT));
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "address")),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_LEFT, 2));

			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil.createParagraphText(fontText, WARN_PRIOD),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_RIGHT));
			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, getDateToString(map.get("startDate")) + "至"
							+ getDateToString(map.get("endDate"))), new int[] { 4, 4, 4, 2 }, Element.ALIGN_LEFT));

			table.addCell(HcpmsPdfCellUtil.createCellRow(HcpmsParagraphUtil.createParagraphText(fontText, WARN_RESULT),
					Element.ALIGN_CENTER, 3));

			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil.createParagraphText(fontText, UNIT_PROBLEM),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_RIGHT));

			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "problem")),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_LEFT, 3));

			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, UNIT_AMOUNT + getValue(map, "amount") + "  元"),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_LEFT, 4));

			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil.createParagraphText(fontText, UNIT_DICIDE),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_RIGHT));
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "decide")),
					new int[] { 4, 4, 4, 2 }, Element.ALIGN_LEFT, 3));

			float[] widthRow1 = { 0.6f, 0.4f };

			PdfPTable tableRow2 = new PdfPTable(widthRow1);
			tableRow2.setWidthPercentage(100);
			tableRow2.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			tableRow2.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_FROM_UNIT), new int[] { 4, 4, 4, 2 },
					Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			tableRow2.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_TO_UNIT), new int[] { 4, 4, 4, 1 },
					Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			PdfPCell cell2 = new PdfPCell();
			cell2.setColspan(5);
			cell2.addElement(tableRow2);
			table.addCell(cell2);

			PdfPTable tableRow3 = new PdfPTable(widthRow1);
			tableRow3.setWidthPercentage(100);
			tableRow3.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			tableRow3.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, getDateToString(map.get("noticeDate"))),
					new int[] { 4, 4, 4, 60 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			tableRow3.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_PARENT_DATE), new int[] { 4, 4, 4, 60 },
					Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			PdfPCell cell3 = new PdfPCell();
			cell3.setColspan(5);
			cell3.addElement(tableRow3);
			table.addCell(cell3);

			table.addCell(HcpmsPdfCellUtil.createCellCol(HcpmsParagraphUtil.createParagraphText(fontText, AUDIT_UNIT),
					new int[] { 4, 4, 4, 100 }, Element.ALIGN_CENTER, 5, Rectangle.NO_BORDER));
			document.add(table);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setPdfWriter(PdfWriter pdf, Map<String, Object> map) {
	}

	public static String getValue(Map<String, Object> map, String name) {
		String str = "";
		if (name == null || "".equals(name))
			return str;
		Object obj = map.get(name);
		if (obj == null || "".equals(obj))
			return str;
		if (obj instanceof Date) {
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			str = sft.format(obj);
		} else if (obj instanceof List) {

		} else if (obj instanceof Map) {

		} else {
			str = obj.toString();
		}
		return str;
	}

	public static String getDateToString(Object obj) {
		if (obj == null || "".equals(obj)) {
			return WARN_PARENT_DATE;
		}
		if (obj instanceof Date) {
			SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
			return sft.format(obj);
		} else if (obj instanceof String) {
			Date date = new Date();
			try {
				date = new SimpleDateFormat("yyyy-MM-dd").parse(obj.toString());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
			return sft.format(date);
		}
		return "";
	}
}
